{% extends "base.html" %}

{% block title %}
    添加采购主表
{% endblock %}

{% block main %}
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">添加采购主表</h3>
            </div>
            <div class="card-body">
                <form id="payMainForm" method="post" action="{% url 'pay_main_add' %}">
                    {% csrf_token %}
                    <div class="form-group">
                        <label for="Eid">员工编号</label>
                        <input type="text" class="form-control" id="Eid" name="Eid" required>
                    </div>
                    <input type="hidden" name="Pcount" value="0">
                    <input type="hidden" name="Ptotal" value="0">
                    <div class="form-group">
                        <label for="Pdate">采购时间</label>
                        <input type="text" class="form-control" id="Pdate" name="Pdate" placeholder="例如20240101" required>
                    </div>
                    <div class="form-group">
                        <label for="other">备注</label>
                        <input type="text" class="form-control" id="other" name="other">
                    </div>
                    <button type="submit" class="btn btn-primary">提交</button>
                </form>
            </div>
        </div>
    </div>
{% endblock %}

{% block script %}
    <script>
        $(document).ready(function () {


            // 表单提交事件
            $('#payMainForm').submit(function (event) {
                event.preventDefault();
                $.ajax({
                    type: 'POST',
                    url: $(this).attr('action'),
                    data: $(this).serialize(),
                    success: function (response) {
                        if (response.code === 200) {
                            Swal.fire({
                                icon: 'success',
                                title: response.message,
                                showConfirmButton: false,
                                timer: 1500
                            }).then(function () {
                                window.location.href = '/pay_main_list';  // 成功后跳转到列表页面
                            });
                        } else {
                            Swal.fire({
                                icon: 'error',
                                title: '添加失败',
                                text: response.message,
                            });
                        }
                    },
                    error: function () {
                        Swal.fire({
                            icon: 'error',
                            title: '添加失败',
                            text: '服务器错误，请稍后重试。',
                        });
                    }
                });
            });
        });
    </script>
{% endblock %}
